<template>
  <el-dialog :title="formData.id?'编辑客户':'添加客户'" :visible="showDialog" @close="btnCancel">
    <!-- :rules="rules" -->
    <el-form ref="addForm" label-width="120px" :model="formData" label-position="top">
      <el-form-item label="经纪" prop="agent">
        <el-select v-model="formData.agent" placeholder="select">
          <el-option v-for="item in resagent" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="formData.wechat" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <imageUpload ref="imageupload" />
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import imageUpload from '@/components/ImageUpload'
import { addClients, getAgents, postdio } from '@/api/client'

export default {
  components: {
    imageUpload
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userList: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '',
        email: '',
        phone: '',
        wechat: '',
        image: '',
        desc: '',
        agent: ''
      },
      resagent: [],
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        email: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        phone: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        wechat: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
        // agentDesc: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  watch: {
    userList() {
      console.log(this.userList.image)
      // this.$refs.imageupload.fileList = []
      // 监听数据发生变化时,把数据回显
      if (this.userList.image) {
        this.$refs.imageupload.fileList = [{
          url: this.userList.image
        }]
        this.formData = this.userList
      }
      console.log(97, this.userList)
    }
  },
  async created() {
    const res = await getAgents()
    this.resagent = res.data
    console.log(90, res)
  },

  methods: {
    btnCancel() {
      this.formData = {
        name: '',
        email: '',
        phone: '',
        wechat: '',
        image: '',
        desc: ''
      }
      // 清空表单数据
      this.$refs.addForm.resetFields()
      this.$emit('update:showDialog', false)
      this.$refs.imageupload.fileList = []
    },

    async btnOk() {
      // 校验表单
      // await this.$refs.addForm.validate()
      console.log(1)
      this.formData.image = this.$refs.imageupload.fileList[0].url

      this.$message.success('操作成功')
      // 关闭弹窗,props不能更改,只能通知父组件关闭

      if (this.formData.id) {
        await postdio(this.formData)
      } else {
        await addClients(this.formData)
      }
      // this.$emit('closeDialog')
      // 固定写法,优化通知父组件关闭按钮
      this.$emit('update:showDialog', false)
      // 通知父组件更新
      this.$emit('addDepartments')
      this.$refs.imageupload.fileList = []
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
